//
//  HMEmoticonKeyBoardViewModel.swift
//  Weibo02
//
//  Created by teacher on 16/4/26.
//  Copyright © 2016年 teacher. All rights reserved.
//  表情键盘所做的一些逻辑操作：读取表情，截取表情，保存最近表情

import UIKit


// 每一页有多少列
let HMEmoticonKetboardMaxCol = 7
// 每一页有多少行
let HMEmoticonKetboardMaxRow = 3
// 每一个页显示多少个表情按钮
let HMEmoticonKetboardPageEmticonCount = HMEmoticonKetboardMaxCol * HMEmoticonKetboardMaxRow - 1

class HMEmoticonKeyBoardViewModel: NSObject {
    
    
    // 单例
    static let sharedViewModel: HMEmoticonKeyBoardViewModel = HMEmoticonKeyBoardViewModel()

    // 表情的bundle
    lazy var emoticonBundle: NSBundle = {
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        return NSBundle(path: path)!
    }()
    
    // 定义一个数组记录默认表情
    
    lazy var defaultEmoticons: [HMEmoticon] = {
        
        return self.emoticonsWithPath("default/info.plist")
//        // 1. 取到默认表情的info.plist路径
//        let path = self.emoticonBundle.pathForResource("default/info.plist", ofType: nil)!
//        
//        // 2. 读取字典数组
//        let dicts = NSArray(contentsOfFile: path)!
//        
//        // 3. 初始化一个模型数组以保存模型
//        var tempArray = [HMEmoticon]()
//        
//        // 4. 字典转模型
//        for value in dicts {
//            let model = HMEmoticon(dict: (value as! [String : AnyObject]))
//            tempArray.append(model)
//        }
//        
//        // 5. 返回
//        return tempArray
    }()
    
    
    /// emoji表情
    lazy var emojiEmoticons: [HMEmoticon] = {
        return self.emoticonsWithPath("emoji/info.plist")
    }()
    
    /// 浪小花
    lazy var lxhEmoticons: [HMEmoticon] = {
        return self.emoticonsWithPath("lxh/info.plist")
    }()
    
    
    /// collectionView所需要的数组
    lazy var allEmoticons: [[[HMEmoticon]]] = {
    
        return [
            [[HMEmoticon]()],
            self.emoticonPageArray(self.defaultEmoticons),
            self.emoticonPageArray(self.emojiEmoticons),
            self.emoticonPageArray(self.lxhEmoticons),
        ]
    }()
    
    
    // 切割表情：把108个表情切割成 6页，每一页最多20个
    private func emoticonPageArray(emoticons: [HMEmoticon]) -> [[HMEmoticon]]   {
        
        
        // 求出表情有多少页（20个每页）
        let pageCount = (emoticons.count - 1) / 20 + 1
        
        // 定义返回数组
        var result: [[HMEmoticon]] = [[HMEmoticon]]()
        
        // 遍历截取每一页的内容
        for i in 0..<pageCount {
            // 从哪儿开始截取
            let loc = 20 * i
            // 截取的长度
            var len = 20
            
            // 判断是否越界
            if loc + len > emoticons.count {
                len = emoticons.count - loc
            }
            
            let childArray = (emoticons as NSArray).subarrayWithRange(NSMakeRange(loc, len)) as! [HMEmoticon]
            result.append(childArray)
        }
        return result
    }
    
    /// 通过一个路径，获取该路径对应的表情数据
    ///
    /// - parameter path: <#path description#>
    ///
    /// - returns: <#return value description#>
    private func emoticonsWithPath(filePath: String) -> [HMEmoticon] {
        
        // 1. 取到默认表情的info.plist路径
        let path = self.emoticonBundle.pathForResource(filePath, ofType: nil)!
        
        // 2. 读取字典数组
        let dicts = NSArray(contentsOfFile: path)!
        
        // 3. 初始化一个模型数组以保存模型
        var tempArray = [HMEmoticon]()
        
        // 4. 字典转模型
        for value in dicts {
            let model = HMEmoticon(dict: (value as! [String : AnyObject]))
            
            // 设置该表情对应的文件夹路径
            let folderName =  (filePath as NSString).stringByDeletingLastPathComponent
            model.folderName = folderName
            
            tempArray.append(model)
        }
        
        // 5. 返回
        return tempArray
        
    }
    
    
    



}
